package com.ry.common.utils;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class JedisUtils {

	@Autowired
	private JedisPool jedisPool;
	
	public Jedis getResource() {
		Jedis jedis = jedisPool.getResource();
		return jedis;
	}
	
	public byte[] set(byte []key,byte []value) {
		Jedis jedis = getResource();
		try {
			jedis.set(key, value);
			return value;
		}finally {
			jedis.close();
		}
	}
	
	public void expire(byte []key, int time) {
		Jedis jedis = getResource();
		try {
			jedis.expire(key, time);
		}finally {
			jedis.close();
		}
	}
	
	public byte[] get(byte []key) {
		Jedis jedis = getResource();
		try {
			return jedis.get(key);
		}finally {
			jedis.close();
		}
	}
	
	public void del(byte []key) {
		Jedis jedis = getResource();
		try {
			jedis.del(key);
		}finally {
			jedis.close();
		}
	}
	
	public Set<byte[]> keys(String shiro_prefix){
		Jedis jedis = getResource();
		try {
			return jedis.keys((shiro_prefix + "*").getBytes());
		}finally {
			jedis.close();
		}
	}
	
}
